package org.cwnuit.train.acton;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Stime;
import org.cwnuit.train.service.StimeService;
import org.cwnuit.train.service.WtypeService;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.utils.Today;

@SuppressWarnings("all")
public class StimeAction extends HttpServlet{
	
	private StimeService stimeService = new StimeService();
	private WtypeService wtypeService = new WtypeService();
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
        if (cmd.equals("list")) {
        	request.setAttribute("content_tittle", "里程管理");
        	request.setAttribute("pm",listWtype(request));
			request.getRequestDispatcher("web/stime/list.jsp").forward(request, response);
		}else if (cmd.equals("adding")) {
        	request.setAttribute("content_tittle", "里程添加");
        	request.setAttribute("wtypes", wtypeService.findWtypes("SELECT * FROM Wtype ORDER BY wid ASC"));
			request.getRequestDispatcher("web/stime/input.jsp").forward(request, response);
		}else if (cmd.equals("input")) {
        	request.setAttribute("content_tittle", "里程编辑");
			if (request.getParameter("sid") == null||request.getParameter("sid").equals("")) {
				if (addStime(request)) {
					request.setAttribute("msg", "恭喜你，添加成功！");
					request.getRequestDispatcher("stime?cmd=list").forward(
							request, response);
				}else{
					request.setAttribute("msg", "对不起！该里程名称已经存在，信息添加失败！");
					request.getRequestDispatcher("web/stime/input.jsp").forward(request, response);
				}
				
			} else {
				if (updateStime(request)) {
					request.setAttribute("msg", "恭喜你，成功修改里程！");
				}else {
					request.setAttribute("msg", "对不起，里程修改未成功！");
				}
				request.getRequestDispatcher("stime?cmd=list").forward(request, response);
			}
		} else if(cmd.equals("delete")){
			if(deleteStime(request)){
				request.setAttribute("msg", "恭喜你，你已经成功删除了一条记录！");
			} else {
				request.setAttribute("msg", "对不起，你删除记录失败！");
			}
			request.getRequestDispatcher("stime?cmd=list").forward(request, response);
		} else if(cmd.equals("updating")){
			int sid = Integer.parseInt(request.getParameter("sid"));
			request.setAttribute("stime", findStime(sid));
			request.setAttribute("wtypes", wtypeService.findWtypes("SELECT * FROM Wtype ORDER BY wid ASC"));
			request.setAttribute("content_tittle", "修改里程信息");
			request.getRequestDispatcher("web/stime/input.jsp").forward(request, response);
		}
		
	}
	
//////////////////////////////////////////////////工具方法////////////////////////////////////////////////	
	private PageModel listWtype(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Stime ORDER BY wid,sid DESC";
		PageModel pm = stimeService.findByPage(offset, 10, SQL);
		return pm;
	}
	
	private boolean addStime(HttpServletRequest request) {
		
		int wid = Integer.parseInt(request.getParameter("wid"));
		int sstate = Integer.parseInt(request.getParameter("sstate"));
		String sname = request.getParameter("sname");
		String SQL = "INSERT INTO Stime(wid,sstate,sname,saddtime) VALUES(" +wid
				+ "," + sstate 
				+ ",'" + sname + "','"+new Today().getAllDateOfNum()+"')";
		return stimeService.addStime(SQL);
	}

	private boolean updateStime(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		int wid = Integer.parseInt(request.getParameter("wid"));
		int sstate = Integer.parseInt(request.getParameter("sstate"));
		String sname = request.getParameter("sname");
		String SQL = "UPDATE Stime SET wid = " + wid + ",sstate = "
				+ sstate +",sname = '" +sname+ "' WHERE sid = " +sid;
		return stimeService.updateStime(SQL);
	}
	
	private boolean deleteStime(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String SQL = "DELETE FROM Stime WHERE sid = " + sid;
		return stimeService.deleteStime(SQL);
	}
	
	private Stime findStime(int sid) {
		String SQL = "SELECT * FROM Stime WHERE sid = " + sid;
		return stimeService.findStime(SQL);
	}
	
}
